package cn.xingzhe.monster.user.infrastructure.config;

import cn.xingzhe.monster.user.domain.repository.CaptchaRepository;
import cn.xingzhe.monster.user.domain.repository.PermissionRepository;
import cn.xingzhe.monster.user.domain.repository.RoleRepository;
import cn.xingzhe.monster.user.domain.repository.UserRepository;
import cn.xingzhe.monster.user.domain.service.CaptchaDomainService;
import cn.xingzhe.monster.user.domain.service.PermissionDomainService;
import cn.xingzhe.monster.user.domain.service.RoleDomainService;
import cn.xingzhe.monster.user.domain.service.UserDomainService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 领域服务配置
 * 将领域服务注册为 Spring Bean
 *
 * @author System
 * @version 1.0.0
 * @since 2024-01-01
 */
@Configuration
public class DomainServiceConfig {

    /**
     * 验证码长度，默认6位
     */
    @Value("${captcha.length:6}")
    private int captchaLength;

    /**
     * 验证码有效期（分钟），默认5分钟
     */
    @Value("${captcha.expire-minutes:5}")
    private int expireMinutes;

    /**
     * 用户领域服务
     *
     * @param userRepository 用户仓储
     * @return UserDomainService
     */
    @Bean
    public UserDomainService userDomainService(UserRepository userRepository) {
        return new UserDomainService(userRepository);
    }

    /**
     * 角色领域服务
     *
     * @param roleRepository 角色仓储
     * @return RoleDomainService
     */
    @Bean
    public RoleDomainService roleDomainService(RoleRepository roleRepository) {
        return new RoleDomainService(roleRepository);
    }

    /**
     * 验证码领域服务
     *
     * @param captchaRepository 验证码仓储
     * @return CaptchaDomainService
     */
    @Bean
    public CaptchaDomainService captchaDomainService(CaptchaRepository captchaRepository) {
        return new CaptchaDomainService(captchaRepository, captchaLength, expireMinutes);
    }

    /**
     * 权限领域服务
     *
     * @param userRepository        用户仓储
     * @param roleRepository        角色仓储
     * @param permissionRepository 权限仓储
     * @return PermissionDomainService
     */
    @Bean
    public PermissionDomainService permissionDomainService(UserRepository userRepository,
                                                           RoleRepository roleRepository,
                                                           PermissionRepository permissionRepository) {
        return new PermissionDomainService(userRepository, roleRepository, permissionRepository);
    }
}

